﻿(function() {

var message = {
};

message.FindImage = chrome.i18n.getMessage("FindImage");

var gRule = new Rule();

gRule.loadFromDb();

console.debug("load background.js");

/* recv request from chrome.extension.sendRequest in content.js */
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
	var tab = sender.tab;
	// console.log("%s %s", tab.id, request.cmd);
	if (tab.id >= 0) { // form page or options
		var cmd = request.cmd;
		if (!cmd) {
			return;
		}
		if (cmd == "log") {
			var level = request.level || "log";
			var log = request.data;
			do {
				if (!log) {
					break;
				}
				var time = time_to_str(new Date());
				var str = sprintf("[%s] (%s): %s", time, tab.url, log);
				switch (level) {
				case "debug":
					console.debug(str);
					break;
				case "log":
					console.log(str);
					break;
				}
				
			} while (0);
		} else if (cmd == "load_rule" || cmd == "reload_rule") {
			gRule.loadFromDb();
		}
	} else {// maybe popup
	}
});

/* 
 * mask some request like ads by url
**/
chrome.webRequest.onBeforeRequest.addListener(
	function(details) {
		// console.debug("check url: %s, type: %s", details.url, details.type);
		// var cancel = false;
		// var rule = gRule.getRuleByUrl(details.url);
		var cancel = gRule.getFlagByUrl(details.url, "cancel");
		// if (rule && rule.cancel) {
		if (cancel) {
			// cancel = true;
			console.log("block url: %s, type: %s", details.url, details.type);
		} else {
			// console.debug("ignore url: %s", details.url, details.type);
		}
		return { "cancel": cancel };
	},
	{ 
		"urls": [ "<all_urls>" ],
		"types": [ "image", "object" ]
	},
	[ "blocking" ]
);

/*
 * modify request header by url
 * if url is match in lib, modify some header
**/
chrome.webRequest.onBeforeSendHeaders.addListener(
	function(details) { 
		var headers = details.requestHeaders;
		do {
			if (!details || !details.url) {
				break;
			}
			var rule = gRule.getRuleByUrl(details.url);
			if (!rule) {
				break;
			}
			// console.debug("url: %s, type: %s, num: %d", details.url, details.type, headers.length);
			for (var i = 0; i < headers.length; i++) {
				if (headers[i].name == "Referer") {
					if (rule.referer) {
						console.debug("set referer from %s to %s", headers[i].value, rule.referer);
						headers[i].value = rule.referer;
					} else {
						console.debug("remove referer %s", headers[i].value);
						headers.splice(i, 1);
					}
				}
			}
			if (!rule.cache) {
				console.log("no-cache");
				headers.push(
					{ 
						"name": "Cache-Control", 
						"value": "no-cache, must-revalidate" 
					}
				);
			}
		} while (0);
		return { "requestHeaders": headers };
	},
	{ 
		"urls": [ "<all_urls>" ],
		"types": [ "main_frame", "sub_frame", "image", "object" ]
	},
	[ "blocking", "requestHeaders" ] // remober the blocking, or take no effect
);

chrome.contextMenus.create(
	{
		"type": "normal",
		"title": "屏蔽图片",
		"contexts": [ "all" ],
		"onclick": function(info, tab) {
			// console.log("src: %s", info.srcUrl);
			// alert(info.srcUrl);
			var rule = {
				"name": info.srcUrl,
				"inc": info.srcUrl,
				"exc": "",
				"cancel": true,
				"cache": true
			};
			gRule.saveRule(rule);
		}
	}, 
	function() {
	}
);

})();
